/**
* eCommerce Application Sample for J2EE Training 
* Implementation of AccountBean
* @author Fabienne Boyer - Didier Donsez
* may 2006
*/

package ecom.beans;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Entity
@NamedQuery(name = "AllAccounts", query = "select a from AccountBean a")
public class AccountBean implements java.io.Serializable{
    /**
	 * Account Bean UID (for serialization)
	 */
	private static final long serialVersionUID = 7903111151206162149L;
	//Account Identifier (Primary key)
	@Id
	@GeneratedValue
	int accountId;
    
	//Account owner id
	//int accountOwnerId;
	String emailId;
	
	//Account balance
	double balance;
	
	
	public AccountBean(){
	}
	
	/*public AccountBean(int accountId, int accountOwnerId, double balance) {
		this();
		//this.accountId = accountId;
		//this.accountOwnerId = accountOwnerId;
		this.email = email;
		this.balance = balance;
	}*/
	
	public AccountBean(String emailId, double balance) {
		this();
		//this.accountId = accountId;
		//this.accountOwnerId = accountOwnerId;
		this.emailId = emailId;
		this.balance = balance;
	}
	
	/*public int getAccountId() {
		return accountId;
	}
	public void setAccountId(int accountId) {
		this.accountId = accountId;
	}
	public int getAccountOwner() {
		return accountOwnerId;
	}
	public void setAccountOwner(int accountOwner) {
		this.accountOwnerId = accountOwner;
	}*/
	
	public String getEmailId() {
		return emailId;
	}
	public void setEmailId(String emailId) {
		this.emailId = emailId;
	}
	
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	
	/*public String toString() {
			return Integer.toString(accountId)+ " : " + Integer.toString(accountOwnerId);
	}*/
	
	public String toString() {
		return emailId + " : " + Double.toString(balance);
}
	
	/**
	 * Withdraw amount euros from the account, if possible
	 * @param amount The amount to withdraw
	 * @return true if the withdrawal was possible, false if the balance was insufficient
	 */
	public boolean withdraw(double amount) {
		if (balance - amount >= 0)
		{
			balance -= amount;
			return true;
		}
		else return false;
	}
	
	public void deposit(double amount) {
		balance += amount;
	}
}
